﻿#region

using System;
using System.Xml;
using Styx.Logic.BehaviorTree;

#endregion

namespace Ink_Master.Bots.InkMaster.InfoBuilders
{
    public static class WoWHead
    {
        /// <summary>
        ///   Gets the pigment qty from inkid.
        /// </summary>
        /// <param name = "inkid">The inkid.</param>
        /// <returns></returns>
        public static int GetPigmentQtyFromInkid(int inkid)
        {
            // Create a new XmlDocument  
            try
            {
                XmlTextReader reader = new XmlTextReader(@"http://www.wowhead.com/item=" + inkid + "&xml");
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            {
                                if (reader.HasAttributes && reader.Name == "reagent")
                                {
                                    for (int i = 0; i < reader.AttributeCount; i++)
                                    {
                                        reader.MoveToAttribute(i);
                                        if (reader.Name == "count")
                                        {
                                            return Convert.ToInt32(reader.Value);
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
                return 0;
            }
            catch (Exception e)
            {
                Log.Slog("Exception Caught in GetPigmentQtyFromInkID: " + e);
                TreeRoot.Stop();
                return 0;
            }
        }


        /// <summary>
        ///   Gets the pigment from ink ID.
        /// </summary>
        /// <param name = "inkid">The Ink ID.</param>
        /// <returns></returns>
        public static string GetPigmentFromInkID(int inkid)
        {
            // Create a new XmlDocument  
            try
            {
                XmlTextReader reader = new XmlTextReader(@"http://www.wowhead.com/item=" + inkid + "&xml");
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            {
                                if (reader.HasAttributes && reader.Name == "reagent")

                                {
                                    for (int i = 0; i < reader.AttributeCount; i++)
                                    {
                                        reader.MoveToAttribute(i);
                                        if (reader.Name == "name")
                                        {
                                            return reader.Value;
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }


                return "";
            }
            catch (Exception e)
            {
                Log.Slog("Exception Caught in GetPigmentFromInkID: " + e);
                TreeRoot.Stop();
                return "";
            }
        }
    }
}